package com.ch999.detect.View.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ch999.detect.R;
import com.ch999.detect.mode.bean.PhoneGoodsMode;

import java.util.List;

/**
 * Created by Administrator on 2016/12/13 0013.
 */

public class JiXinNameAdatper extends RecyclerView.Adapter<JiXinNameAdatper.ViewHolder> {
    public Context mContext;
    public List<PhoneGoodsMode.DataBean.ItemsBean> data;

    public JiXinNameAdatper(Context context, List<PhoneGoodsMode.DataBean.ItemsBean> data) {
        this.mContext = context;
        this.data = data;
    }

    public interface OnClickItemListener {
        void onClickItemListener(PhoneGoodsMode.DataBean.ItemsBean databean, int position);
    }

    OnClickItemListener onClickItemListener;

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_phone_name, parent, false));
        return holder;
    }

    public void setData(List<PhoneGoodsMode.DataBean.ItemsBean> data) {

        if (data != null && this.data != null) {
            this.data = data;
            notifyDataSetChanged();
        }

    }

    @Override
    public int getItemCount() {
        if (data == null) {
            return 0;
        }
        return data.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final PhoneGoodsMode.DataBean.ItemsBean datBean = data.get(position);
        holder.tvName.setText(datBean.getPname());

        if (datBean.isSelected()){
            holder.tvName.setTextColor(mContext.getResources().getColor(R.color.es_g));
        }else {
            holder.tvName.setTextColor(mContext.getResources().getColor(R.color.es_b));
        }

        holder.llRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickItemListener != null) {
                    onClickItemListener.onClickItemListener(datBean,position);
                }
            }
        });

    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        RelativeLayout llRootView;

        ViewHolder(View view) {
            super(view);
            tvName = (TextView)view.findViewById(R.id.tv_name);
            llRootView = (RelativeLayout)view.findViewById(R.id.ll_root_view);
        }
    }
}
